﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NewBee.Core.Data;
using NewBee.Core.Data.Model;
using NewBee.Core.Models;

namespace NewBee.Core.DataService
{
    public class AdminLoginLogDS : BaseDataService
    {
        /// <summary>
        /// 表名
        /// </summary>
        public override string TableName
        {
            get { return "AdminLoginLog"; }
        }


        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        public IEnumerable<AdminLoginLog> Get()
        {
            SQLHelper sql = new SQLHelper(TableName).Order("LogID", true);

            return Query<AdminLoginLog>(sql.ToString(), null);
        }

        /// <summary>
        /// 分页搜索查找
        /// </summary>
        /// <param name="page"></param>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public PageResult Search(int page, string AppID)
        {
            PageParam _PageParam = new PageParam(20, page).Where(AppID.Length > 0 ? string.Format(" AppID LIKE '%{0}%' ", AppID) : string.Empty).Order("LogID", true);
           
            PageResult result = QueryPage(_PageParam);

            return result;
        }

        /// <summary>
        /// 查询ID单条
        /// </summary>
        /// <returns></returns>
        public AdminLoginLog Get(long id)
        {
            SQLHelper sql = new SQLHelper(TableName).Where(new List<string> { "LogID" });

            var list = QueryFirst<AdminLoginLog>(sql.ToString(), new { LogID = id });

            return list;
        }

        /// <summary>
        /// 查询统计信息
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CourseCount> GetSum()
        {
            SQLHelper sql = new SQLHelper(TableName).Columns(new List<string> { "LogID", "SUM(1) AS SumT" }).Group("LogID").Order("LogID", true).Order("SUM(1)", false);

            var list = Query<CourseCount>(sql.ToString(), null);

            return list;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Insert(AdminLoginLog model)
        {
            var columns = new List<string> { "AppID", "ExamSiteID", "LoginIPAddress", "LoginTime", "LoginStatus" };

            SQLHelper sql = new SQLHelper(TableName).GreateInsert(columns);

            return Execute(sql, new
            {
                AppID = model.AppID,
                ExamSiteID = model.ExamSiteID,
                LoginIPAddress = model.LoginIPAddress,
                LoginTime = model.LoginTime,
                LoginStatus = model.LoginStatus
            });
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(AdminLoginLog model)
        {
            var editParamaters = new List<string> { "AppID", "ExamSiteID", "LoginIPAddress", "LoginTime", "LoginStatus" };
            var whereParamters = new List<string> { "LogID" };

            SQLHelper sql = new SQLHelper(TableName).GreateUpdate(editParamaters, whereParamters);

            return Execute(sql, new
            {
                LogID = model.LogID,
                AppID = model.AppID,
                ExamSiteID = model.ExamSiteID,
                LoginIPAddress = model.LoginIPAddress,
                LoginTime = model.LoginTime,
                LoginStatus = model.LoginStatus
            });
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(long id)
        {

            //var whereParamters = new List<string> { "LogID" };

            //SQLHelper sql = new SQLHelper(TableName).GreateDelete(whereParamters);

            //return Execute(sql, new
            //{
            //    LogID = id
            //});

            return this._DbHelper.Delete(TableName).Where("LogID = @0").Parameters(id).Execute();
        }
    }
}
